class CommentController < ApplicationController
  layout "main", :except => [:new, :edit, :reply, :delete, :delete_reason]
  before_filter :login_required
  require_role ['Administrator', 'Moderator'], :only => [:edit, :update, :destroy], :unless => "current_user.id == Comment.find(params[:id]).user_id"
  def reply
    @replyingto = Comment.find(params[:parent_id])
    @comment = Comment.new()
    @comment.text = "\n[quote=\""+@replyingto.user.login+"\"]\n"+clean_text(@replyingto.text)+"\n[/quote]\n\n"
  end

  def new
  end

  def change_rating
    if current_user
      @rate_value = (current_user.has_role?(['Administrator', 'Moderator'])) ? 5 : 1
      @comment = Comment.find(params[:id], :include => [:comment_rating_logs])
      if has_not_rated_comment?(@comment)
        if params[:direction] == "down"
          new_rating = @comment[:rating] - @rate_value
          @comment.update_attributes(:rating => new_rating)
          CommentRatingLog.new(:user_id => current_user.id, :comment_id => @comment[:id], :value => '-1').save
        else
          if params[:direction] == "up"
            new_rating = @comment[:rating] + @rate_value
            @comment.update_attributes(:rating => new_rating)
            CommentRatingLog.new(:user_id => current_user.id, :comment_id => @comment[:id], :value => '1').save
          end
        end
      end
    end
    @comment = Comment.find(params[:id], :include => [:comment_rating_logs])
    render(:partial => 'comment_rating')
  end

  def add
    params[:comment][:user_id]=current_user.id
    params[:comment][:media_id]=params[:id]
    @comment = Comment.new(params[:comment])
    if @comment.save
      flash[:notice] = 'Comment was successfully added.'
      redirect_to :controller => '/media', :action => 'show', :id => params[:id]
    else
      @media = media.find(params[:id])
      @media.increment_views
      @comments = Comment.find(:all, :conditions => ["media_id = ? and parent_id = 0", params[:id]])
      @comment_threshold = -5
      render '/media/show', :id => params[:id]
    end
  end
  
  def edit
    @comment = Comment.find(params[:id])
  end
  
  def update
    @comment = Comment.find(params[:id])
    @media_id = @comment.media_id
    if @comment.update_attributes(params[:comment])
      flash[:notice] = 'Comment was successfully updated.'
      redirect_back_or_default :controller => '/media', :action => 'show', :id => @media_id
    else
      render :action => 'edit'
    end
  end

  def delete_reason
    @comment = Comment.find(params[:id])
  end

  def delete
    @comment = Comment.find(params[:id])    
  end

  def destroy
    @comment = Comment.find(params[:id])
    @media_id = @comment.media_id
    for child in @comment.all_children
      child.update_attributes(:deleted_by => current_user.id, :deleted_at => Time.now(), :deleted_reason => params[:comment][:deleted_reason])
    end
    if @comment.update_attributes(:deleted_by => current_user.id, :deleted_at => Time.now(), :deleted_reason => params[:comment][:deleted_reason])
      flash[:notice] = 'Comment was successfully deleted.'
      redirect_back_or_default :controller => '/media', :action => 'show', :id => @media_id
    else
      flash[:notice] = 'Comment was not deleted.'
      redirect_back_or_default :controller => '/media', :action => 'show', :id => @media_id
    end
  end
  def undestroy
    @comment = Comment.find(params[:id])
    @media_id = @comment.media_id
    if @comment.update_attributes(:deleted_by => nil, :deleted_at => nil, :deleted_reason => nil)
      flash[:notice] = 'Comment was successfully undeleted.'
      redirect_back_or_default :controller => '/media', :action => 'show', :id => @media_id
    else
      flash[:notice] = 'Comment was not undeleted.'
      redirect_back_or_default :controller => '/media', :action => 'show', :id => @media_id
    end
  end
  
  def preview
    @content = params[:content]
    render :partial => "preview"
  end
end
